package com.qeegoo.b2bautozityp.moudle.detail.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DataSource;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DetailBean;
import com.qeegoo.b2bautozityp.moudle.detail.bean.WareHouseBean;
import com.qeegoo.b2bautozityp.moudle.detail.bean.NetHelper;
import com.qeegoo.b2bautozityp.moudle.detail.view.OrderDetailsActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HongQian.Wang on 2017/10/19.
 */

public class ExpandAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    //供应商商品属性列表  仓库列表


    private List<String> mSelectList = new ArrayList<>();
    private PageAdapter pageAdapter;
    //
    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> mSelectedDataList=new ArrayList<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean>();
    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> mWrapperSelectedDataList = new ArrayList<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean>();

    private List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> mAddDataList = new ArrayList<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean>();
    private ExpandableListView expandLv;
    private Button btn_submit;

    public ExpandAdapter(Button btn_submit, PageAdapter pagerAdapter, ExpandableListView expandLv) {
        this.pageAdapter = pagerAdapter;
        this.expandLv = expandLv;
        this.btn_submit = btn_submit;
        btn_submit.setOnClickListener(this);
    }


    @Override
    public int getGroupCount() {
        return mSelectedDataList.size() + mAddDataList.size();
    }

    @Override
    public int getChildrenCount(int parentPos) {
        return 1;
    }

    @Override
    public Object getGroup(int parentPos) {
        return null;
    }

    @Override
    public Object getChild(int parentPos, int childPos) {
        return null;
    }

    @Override
    public long getGroupId(int psrentPos) {
        return 0;
    }

    @Override
    public long getChildId(int parentPos, int childPos) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int parentPos, boolean isExpand, View view, ViewGroup viewGroup) {
        View groupView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_group, viewGroup, false);
        TextView tv_group_name = (TextView) groupView.findViewById(R.id.tv_group_name);
        ImageView ic_arrow_up = (ImageView) groupView.findViewById(R.id.ic_arrow_up);
        TextView ib_delete = (TextView) groupView.findViewById(R.id.ib_delete);
        LinearLayout ll_group_collapse = (LinearLayout) groupView.findViewById(R.id.ll_group_collapse);
        DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean bean = null;
        if (parentPos < mSelectList.size()) {
            bean = mSelectedDataList.get(parentPos);

            tv_group_name.setText(bean.getGoodsName());
        } else {
            bean = mAddDataList.get(parentPos - mSelectList.size());
        }


        if (isExpand) {
            ic_arrow_up.setBackgroundResource(R.mipmap.ic_arrow_up);
        } else {
            ic_arrow_up.setBackgroundResource(R.mipmap.ic_arrow_down);
        }
        ib_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (parentPos < mSelectedDataList.size()) {
                    mSelectedDataList.remove(mSelectedDataList.get(parentPos));
                    mSelectList.remove(mSelectList.get(parentPos));
                    pageAdapter.notifyDataSetChanged();
                } else {
                    mAddDataList.remove(mAddDataList.get(parentPos - mSelectedDataList.size()));
                }
                notifyDataSetChanged();
            }
        });
        return groupView;
    }

    @Override
    public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup parent) {
        View childView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_child, parent, false);
        View ll_diff = childView.findViewById(R.id.ll_diff);
        ImageView ivgoodsdelete3 = (ImageView) childView.findViewById(R.id.iv_goods_delete3);
        ImageView ivgoods3 = (ImageView) childView.findViewById(R.id.iv_goods3);
        ImageView ivgoodsdelete2 = (ImageView) childView.findViewById(R.id.iv_goods_delete2);
        ImageView ivgoods2 = (ImageView) childView.findViewById(R.id.iv_goods2);
        ImageView ivgoodsdelete1 = (ImageView) childView.findViewById(R.id.iv_goods_delete1);
        ImageView ivgoods1 = (ImageView) childView.findViewById(R.id.iv_goods1);
        TextView tvaddress = (TextView) childView.findViewById(R.id.tv_address);
        EditText etday = (EditText) childView.findViewById(R.id.tv_day);
        RadioGroup rbgroup = (RadioGroup) childView.findViewById(R.id.rb_group);
        RadioButton rborder = (RadioButton) childView.findViewById(R.id.rb_order);
        RadioButton rbhave = (RadioButton) childView.findViewById(R.id.rb_have);
        EditText etprice = (EditText) childView.findViewById(R.id.et_price);
        TextView tvbrand = (TextView) childView.findViewById(R.id.tv_brand);
        TextView tvquality = (TextView) childView.findViewById(R.id.tv_quality);
        EditText ettype = (EditText) childView.findViewById(R.id.et_type);
        EditText etoe = (EditText) childView.findViewById(R.id.et_oe);
        DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean bean = null;
        if (parentPos < mSelectedDataList.size()) {
            bean = mSelectedDataList.get(parentPos);
        } else {
            bean = mAddDataList.get(parentPos - mSelectedDataList.size());
            ll_diff.setVisibility(View.VISIBLE);
        }
        tvquality.setText(bean.getGoodsTypeStr());  //设置配件品质

        tvbrand.setText(bean.getBrandName());  //设置品牌
        etprice.setText(bean.getPrice());   //设置价格
        if (TextUtils.isEmpty(bean.getAvlNum())) {
            rborder.setChecked(true);
        } else {                          //设置库存 单选按钮
            rbhave.setChecked(true);
        }
        etday.setText(bean.getArriveDay());   // 设置  几天发货
        tvaddress.setText(bean.getAreaName());   //设置发货地址

        etoe.addTextChangedListener(new TextWatcher() {  //存储oe
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                     if(parentPos<mSelectedDataList.size()) {
                         mSelectedDataList.get(parentPos).setOem(etoe.getText().toString());
                     }else {
                         mAddDataList.get(parentPos-mSelectedDataList.size()).setOem(etoe.getText().toString());
                     }
            }
        });

        ettype.addTextChangedListener(new TextWatcher() {  //存储规格型号
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(parentPos<mSelectedDataList.size()) {
                    mSelectedDataList.get(parentPos).setGoodsStyle(ettype.getText().toString());
                }else {
                    mAddDataList.get(parentPos-mSelectedDataList.size()).setGoodsStyle(ettype.getText().toString());
                }
            }
        });


        tvquality.setOnClickListener(new View.OnClickListener() {  //存储配件品质的值
            @Override
            public void onClick(View view) {
                OrderDetailsActivity activity = (OrderDetailsActivity) parent.getContext();
                activity.drawer.openDrawer(Gravity.RIGHT);
                NetHelper.listWareHouses(new DataSource.SucceedCallBack<WareHouseBean>() {


                    @Override
                    public void onDataLoaded(WareHouseBean wareHouseBean) {
                        List<WareHouseBean.DataBean> qualityList = wareHouseBean.getData();
                        GoodsQualityAdapter qualityAdapter = new GoodsQualityAdapter(qualityList);
                        activity.slide_view.setAdapter(qualityAdapter);
                        activity.slide_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                WareHouseBean.DataBean qualityBean = qualityList.get(position);
                                tvquality.setText(qualityBean.getName());   //设置选中配件品质的值
                                if(parentPos<mSelectedDataList.size()) {
                                    mSelectedDataList.get(parentPos).setGoodsTypeStr(tvquality.getText().toString());
                                }else {
                                    mAddDataList.get(parentPos-mSelectedDataList.size()).setGoodsTypeStr(tvquality.getText().toString());
                                }
                                activity.drawer.closeDrawer(Gravity.RIGHT);
                            }
                        });

                    }
                });
            }
        });

        tvbrand.setOnClickListener(new View.OnClickListener() {  //存储品牌的值
            @Override
            public void onClick(View view) {
                OrderDetailsActivity activity = (OrderDetailsActivity) parent.getContext();
                activity.drawer.openDrawer(Gravity.RIGHT);
            }
        });
       
        if("2".equals(bean.getAssistType())) {

            etprice.setEnabled(false);
        }else {
            etprice.setEnabled(true);
        }
        
        etprice.addTextChangedListener(new TextWatcher() {  //存储价格的值
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(parentPos<mSelectedDataList.size()) {
                    mSelectedDataList.get(parentPos).setPrice(etprice.getText().toString());
                }else {
                    mAddDataList.get(parentPos-mSelectedDataList.size()).setPrice(etprice.getText().toString());
                }
            }
        });

        etday.addTextChangedListener(new TextWatcher() {  //存储几天发货的值
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(parentPos<mSelectedDataList.size()) {
                    mSelectedDataList.get(parentPos).setArriveDay(etday.getText().toString());
                }else {
                    mAddDataList.get(parentPos-mSelectedDataList.size()).setArriveDay(etday.getText().toString());
                }

            }
        });


        return childView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    public void setSelectedDataAndRefreshed(List<String> mSelectList, List<DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean> mSelectedDataList) {
        this.mSelectList = mSelectList;  //保留选中数据的引用
        this.mSelectedDataList = mSelectedDataList;
//        mWrapperSelectedDataList.clear();
//        mWrapperSelectedDataList.addAll(mSelectedDataList);
        notifyDataSetChanged();
//        //默认展开所有的分组
//        for (int i=0;i<mSelectedDataList.size();i++){
//            expandLv.expandGroup(i);
//        }

    }

    public void addItem() {
        DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean bean = new DetailBean.GoodsInfoMoreListBean.AskPriceQuoteGoodsListBean();
        mAddDataList.add(bean);
        notifyDataSetChanged();
    }


    @Override
    public void onClick(View view) {

    }
}
